package com.yyl.fmall.index.controller;

import com.yyl.fmall.index.entity.Category;
import com.yyl.fmall.index.entity.IndexImg;
import com.yyl.fmall.index.service.ICategoryService;
import com.yyl.fmall.index.service.IIndexImgService;
import com.yyl.fmall.product.entity.Product;
import com.yyl.fmall.product.service.IProductService;
import com.yyl.fmall.utils.CommonConstants;
import com.yyl.fmall.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 轮播图  前端控制器
 * </p>
 *
 * @author YuanSir
 * @since 2022-11-04
 */
@RestController
@RequestMapping("/index")
@CrossOrigin
public class IndexImgController {
    
    @Autowired
    private IIndexImgService indexImgService;
    
    @Autowired
    private ICategoryService categoryService;
    
    @Autowired
    private IProductService productService;

    @GetMapping("/indeximg")
    public R indeximg(){
        
        List<IndexImg> data = indexImgService.getIndexImg();
        
        return R.ok().put(CommonConstants.R_MAP_KEY, data);
    
    }
    
    @GetMapping("category-list")
    public R getCategoryList(){
        
        List<Category> categoryList;
        try {
            categoryList = categoryService.getCategoryList();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        return R.ok().put(CommonConstants.R_MAP_KEY, categoryList);
        
    }
    
    @GetMapping("/category-recommends")
    public R categoryRecommends(){
        
        List<Category> categoryList;
        
        try {
            categoryList = categoryService.getCategoryRecommends();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        return R.ok().put(CommonConstants.R_MAP_KEY, categoryList);
        
    }
    
    
    @GetMapping("/list-recommends")
    public R listRecommends(){
        
        List<Product> productList;
        try {
            productList = productService.getListRecommends();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        
        return R.ok().put(CommonConstants.R_MAP_KEY, productList);
        
    }

}
